package com.cn.GenerateJson;

// 文件名：FastjsonFileExample.java
import com.alibaba.fastjson2.JSON;

import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;

public class FastjsonFileExample {
    public static void main(String[] args) {
        Person person = new Person("Bob", 25);
        File file = new File("person_fastjson.json");

        // 写入 JSON 文件
        try (FileWriter writer = new FileWriter(file)) {
            String jsonString = JSON.toJSONString(person, String.valueOf(true)); // true 表示格式化输出
            writer.write(jsonString);
            System.out.println("写入成功: " + file.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 读取 JSON 文件
        try (FileReader reader = new FileReader(file)) {
            Person readPerson = JSON.parseObject(reader, Person.class);
            System.out.println("读取成功: " + readPerson);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}